package com.ww.seckill.rabbitmq;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ww
 */
@Service
@Slf4j
public class MqSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送秒杀信息
     *
     * @param message
     */
    public void sendSecKillMessage(String message) {
        log.info("发送消息：" + message);
        rabbitTemplate.convertAndSend("secKillExchange", "seckill.message", message);
    }



    public void send(Object msg) {
        log.info("发送普通消息：" + msg);
        rabbitTemplate.convertAndSend("queue", msg);
    }

    public void sendFanoutMsg(Object msg) {
        log.info("发送广播消息：" + msg);
        rabbitTemplate.convertAndSend("fanoutExchange", "", msg);
    }


    public void sendDirectMsg01(Object msg) {
        log.info("发送Direct red消息：" + msg);
        rabbitTemplate.convertAndSend("directExchange", "queue.red", msg);
    }

    public void sendDirectMsg02(Object msg) {
        log.info("发送Direct green消息：" + msg);
        rabbitTemplate.convertAndSend("directExchange", "queue.green", msg);
    }


    public void sendTopicMsg01(Object msg) {
        log.info("发送Topic消息(QUEUE01接收):", msg);
        rabbitTemplate.convertAndSend("topicExchange", "queue.red.message", msg);
    }

    public void sendTopicMsg02(Object msg) {
        log.info("发送Topic消息(被两个QUEUE接收):", msg);
        rabbitTemplate.convertAndSend("topicExchange", "message.queue.green", msg);
    }

    public void sendHeaderMsg01(Object msg) {
        log.info("发送(两个队列接收):", msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "fast");
        Message message = new Message(((String) msg).getBytes(), properties);
        rabbitTemplate.convertAndSend("headerExchange", "", message);
    }

    public void sendHeaderMsg02(Object msg) {
        log.info("发送(QUEUE01接收):", msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "normal");
        Message message = new Message(((String) msg).getBytes(), properties);
        rabbitTemplate.convertAndSend("headerExchange", "", message);
    }

}
